﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Register;

namespace BranchOfSansar.Register.CallCenter
{
    public partial class frmComment : Form
    {
        private CommentRow comment = null;
        private long RequestNo = 0;
        public frmComment(long req_no)
        {
            this.RequestNo = req_no;
            InitializeComponent();
            btnDelete.Visible = false;
        }

        public frmComment(CommentRow c)
            : this(c.ReqNo)
        {
            this.comment = c;
            txtComment.Text = this.comment.Comment;
            btnDelete.Visible = true;
        }


        private void frmComment_Load(object sender, EventArgs e)
        {
            txtUser.Text = Globals.UserName;
            txtDate.Text = Globals.SystemDate.ToString("yyyy.MM.dd");

        }

        private void btnComment_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtComment.Text))
            {
                Globals.Warr("Тайлбар оруулаагүй байна!");
                return;
            }

            CallComment save = new CallComment()
            {
                CommentDate = Globals.SystemDate,
                Comment = txtComment.Text,
                UserNo = Globals.UserId,
                ReqNo = RequestNo                
            };
            Result res;
            using (RegisterClient reg = new RegisterClient(Globals.Token))
            {
                //Шинэ
                if (this.comment == null)
                {
                    
                    res = reg.AddComment(save);
                }
                //Засах
                else
                {
                    save.CommentNo = comment.CommentNo;
                    res = reg.EditComment(save);
                }
            }
            if (res.Code != 0)
            {
                Globals.Warr(res.Message);
            }
            else
            {
                Globals.Info(res.Message);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (Globals.Question("Энэ өгөгдөлийг устгахдаа итгэлтэй байна уу?") == System.Windows.Forms.DialogResult.Yes)
                using (RegisterClient reg = new RegisterClient(Globals.Token))
                {
                    CallComment del = new CallComment()
                               {
                                   CommentDate = Globals.SystemDate,
                                   Comment = txtComment.Text,
                                   UserNo = Globals.UserId,
                                   ReqNo = RequestNo,
                                   CommentNo = comment.CommentNo
                               };
                    Result res = reg.DeleteComment(del);

                    if (res.Code != 0)
                    {
                        Globals.Warr(res.Message);
                    }
                    else
                    {
                        Globals.Info(res.Message);
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                }
        }
    }
}
